import { _decorator, Color, Component, EventTouch, Graphics, instantiate, LightProbeGroup, Line, log, Node, Prefab, tween, UITransform, v3, Vec2, Vec3 } from 'cc';
import { DrawingLine } from './DrawingLine';
import { PoolManager } from './PoolManager';
const { ccclass, property } = _decorator;

@ccclass('DrawingLineManager')
export class DrawingLineManager extends Component {

    public static Instance: DrawingLineManager = null;

    @property(Prefab)
    LinePrefab: Prefab = null;

    @property(Prefab)
    ItemPrefab: Prefab = null;

    @property
    CircleRadius: number = 500;

    @property
    Width: number = 10;

    @property
    Radius: number = 350;

    @property
    PointCount: number = 10;

    @property
    MaxCount: number = 1000;

    @property(Node)
    AredNode: Node = null;

    @property(Node)
    StartNode: Node = null;

    public LineNode: Node[] = [];
    public MapLinePos: Map<Node, Vec3[]> = new Map();
    public MapLineColor: Map<Node, Color> = new Map();
    public MapLinGraphics: Map<Node, DrawingLine[]> = new Map();
    public CententPos: Vec3 = new Vec3();
    public IsStart: boolean = false;

    private _curCount: number = 0;

    protected onLoad(): void {
        DrawingLineManager.Instance = this;

        PoolManager.Instance.preload(this.LinePrefab, this.MaxCount);
        this.AredNode.once(Node.EventType.TOUCH_START, this.onTouchStart, this);
        tween(this.StartNode)
            .to(1, { scale: v3(1.2, 1.2, 1.2) }, { easing: `sineIn` })
            .to(1, { scale: v3(1, 1, 1) }, { easing: `sineIn` })
            .union()
            .repeatForever()
            .start();
    }

    protected start(): void {
        this.CententPos = this.node.getWorldPosition().clone();
        this.initItems();
    }

    protected update(dt: number): void {
        this.drawingAllLine();
    }

    drawingAllLine() {
        for (let i = 0; i < this.LineNode.length; i++) {
            const item: Node = this.LineNode[i];
            const pos: Vec3[] = this.MapLinePos.get(item);
            const color: Color = this.MapLineColor.get(item);
            const line: DrawingLine[] = this.MapLinGraphics.get(item)
            for (let index: number = 0; index < pos.length; index++) {
                line[index].drawingLine(pos[index], item.getWorldPosition().clone(), color, this.Width);
            }
        }
    }

    addLine(target: Node, color: Color, vector: Vec2) {
        const targetPos: Vec3 = new Vec3(this.CententPos.x + this.CircleRadius * vector.x, this.CententPos.y + this.CircleRadius * vector.y, this.CententPos.z);
        if (this._curCount >= this.MaxCount) {
            this.MapLinePos.get(target).shift();
            this.MapLinePos.get(target).push(targetPos);
            this.MapLinGraphics.get(target).push(this.MapLinGraphics.get(target).shift());
            return;
        }
        this._curCount++;

        if (!this.MapLinePos.has(target)) {
            this.LineNode.push(target);
            this.MapLinePos.set(target, []);
            this.MapLineColor.set(target, color);
            this.MapLinGraphics.set(target, []);
        }

        this.MapLinePos.get(target).push(targetPos);
        this.MapLinGraphics.get(target).push(this.addGraphics());
    }

    private addGraphics(): DrawingLine {
        const newNode: Node = instantiate(this.LinePrefab);
        newNode.parent = this.node;
        newNode.setPosition(Vec3.ZERO);
        return newNode.getComponent(DrawingLine);
    }

    initItems() {
        const center: Vec3 = new Vec3(0, 0, 0);
        for (let i = 0; i < this.PointCount; i++) {
            // 计算角度（弧度制）
            const angle = (2 * Math.PI / this.PointCount) * i;

            // 圆上坐标 (这里假设在 X-Y 平面)
            const x = center.x + this.Radius * Math.cos(angle);
            const y = center.y + this.Radius * Math.sin(angle);
            const z = center.z;
            const pos = new Vec3(x, y, z);

            const item: Node = instantiate(this.ItemPrefab);
            item.parent = this.node.parent;
            item.setWorldPosition(this.node.getComponent(UITransform)!.convertToWorldSpaceAR(pos));
        }
    }

    protected onDisable(): void {
        PoolManager.Instance.clear(this.LinePrefab.name);
    }

    onTouchStart(event: EventTouch) {
        this.IsStart = true;
        this.AredNode.active = false;
    }

}


